// 如果打开页面时,没有获取到启动信息,则先获取启动信息,才能去请求页面
import store from "../store"
import ajax from './ajax.js'
import config from '../projectInfo.js';
import getIcons from './getIcons.js';
import vuexSave from './vuexSave.js';
export default () => {
	return new Promise((resolve, reject) => {
		if(store.state.$prevLoaded){
			resolve()
			return
		}
		uni.showLoading({
			title: '加载中'
		});
		ajax({
			moduleUrl: true,
			url: '/getVersionList',
			method: 'get',
			data: {
				t: new Date().getTime(),
				tag: store.state.$tag,
				version: store.state.$version,
				pageSize: 1,
				currentPage: 0,
			}
		}).then(res => {
			if (res.code == 200) {
				if(res.data.length > 0){
					var appInfo = JSON.parse(res.data[0].app_info || '{}');
					vuexSave('$appInfo', appInfo);
					var prodArr = ['1', '2', '3'];
					if (prodArr.indexOf(res.data[0].is_publish) > -1) {
						vuexSave('$development', 'prod');
					} else if (appInfo.development) {
						vuexSave('$development', appInfo.development);
					}
					
					var icons = JSON.parse(res.data[0].skin_icon || '[]');
					vuexSave('$icons', getIcons(icons));
					var skinUrl = res.data[0].url
					if(store.state.$skinUrl === skinUrl && store.state.$skinW > 0 && store.state.$skinH > 0){
						console.log('获取缓存的图片尺寸,皮肤尺寸为',store.state.$skinW,store.state.$skinH);
					}else{
						vuexSave('$skinUrl', skinUrl);
						uni.getImageInfo({
							src: store.state.$skinUrl,
							success: image => {
								console.log('获取皮肤图片成功,皮肤尺寸为',image.width,image.height);
								vuexSave('$skinW', image.width);
								vuexSave('$skinH', image.height);
							},
							fail() {
								console.log('获取皮肤图片失败,皮肤路径为',store.state.$skinUrl);
							}
						});
					}
					
					// var aniIcons = JSON.parse(res.data[0].animate_icon || '[]');
					// vuexSave('$aniIcons', getIcons(aniIcons));
					// var aniUrl = res.data[0].animate_url
					// if(store.state.$aniUrl === aniUrl && store.state.$aniW > 0 && store.state.$aniH > 0){
					// 	console.log('获取缓存的动画皮肤尺寸,皮肤尺寸为',store.state.$aniW,store.state.$aniH);
					// }else{
					// 	vuexSave('$aniUrl', aniUrl);
					// 	uni.getImageInfo({
					// 		src: store.state.$aniUrl,
					// 		success: image => {
					// 			console.log('获取动画皮肤成功,皮肤尺寸为',image.width,image.height);
					// 			vuexSave('$aniW', image.width);
					// 			vuexSave('$aniH', image.height);
					// 		},
					// 		fail() {
					// 			console.log('获取动画皮肤失败,皮肤路径为',store.state.$aniUrl);
					// 		}
					// 	});
					// }
					
					vuexSave('$prevLoaded', true);
					resolve()
				}else{
					uni.showToast({
						title: '未获取到页面数据',
						icon: 'none'
					});
					reject()
				}
			}else{
				reject()
			}
		}).finally(() => {
			uni.hideLoading();
		}).catch(()=>{
			reject()
		})
	})
}
